<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        <meta charset="UTF-8">
        <title>欢迎页面-X-admin2.2</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/xadmin.js"></script>
    </head>
    
    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">商品管理</a>
                <a>
                    <cite>商品列表</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">
                            <button class="layui-btn" onclick="xadmin.open('添加商品','./item-add.html',500,500)">
                                <i class="layui-icon"></i>添加商品</button></div>
                        <div class="layui-card-body ">
                            <table id="demo" lay-filter="test" class="layui-table layui-form">
                                                               
                            </table>
                        </div>
                        
                    </div>
                </div>
            </div>
        </div>
    </body>
    <script>
		layui.use(['table','layer'],function(){
			
			var table = layui.table;
			var layer = layui.layer;
			var $ = layui.jquery;
			
			table.render({
				elem:"#demo",
				id:"t1",
				url:'http://localhost:8080/getGoodsPage',
				page:true,
				limit:2,
				limits:[2,4,6,8],
				request:{
					pageName:'pageNo',
					limitName:'pageSize'
				},
				parseData:function(res){
					console.log("res====",res);
					return{
						code:res.code==200?0:res.code,
						msg: res.msg,
						count:res.data.count,
						data:res.data.data
					};
				},
				cols:[[
					{field:'goodsId',title:'ID',sort:true},
					{field:'goodsName',title:'商品名',sort:true},
					{title:'商品分类',sort:true,templet:function(d){
						//console.log("d====",d);
						return d.category.categoryName;	
					}},
					{field:'price',title:'商品价格',sort:true},
					{field:'stock',title:'商品库存',sort:true},
					{field:'createTime',title:'创建时间',sort:true},
					{title:'操作',minWidth:250,templet:function(d){
						return "<button type=\"button\" class=\"layui-btn layui-btn-warm\" lay-event='edit'>编辑</button><button type=\"button\" class=\"layui-btn layui-btn-danger\" lay-event='del'>删除</button><button type=\"button\" class=\"layui-btn\" lay-event='addCar'>加入购物车</button>";
					}},
					
				]]
				
			});
			//工具条绑定
			table.on('tool(test)',function(obj){
				
				console.log("obj=====",obj);
				var i = obj.event;
				
				if(i=='del'){
					layer.confirm("是否删除?",{icon:3,title:'删除提示'},function(index){
						$.getJSON("http://localhost:8080/deleteGoods",{goodsId:obj.data.goodsId},function(d){
							if(d.code!=200){
								layer.msg(d.msg);
							}else{
								obj.del();
								
								layer.close(index);
							}
						});
						
					});
				}else if(i=='edit'){
					//弹出更新页面
					layer.open({
						type:2,
						title:'更新商品',
						area:['500px','500px'],
						shade:0.3,
						anim:2,
						content:"./item-update.html?categoryId="+obj.data.category.categoryId,
						end:function(){
							console.log("更新层被关闭了....");
							//只重载表格
							table.reload("demo");
						},
						success:function(layero,index2){
							var body = layer.getChildFrame("body",index2);
							body.find("#goodsId").val(obj.data.goodsId);
							body.find("#goodsName").val(obj.data.goodsName);
							body.find("#price").val(obj.data.price);
							body.find("#stock").val(obj.data.stock);
							body.find("#createTime").val(obj.data.createTime);
							
						}
					});
					
				}else if(i=='addCar'){
					layer.confirm("是否加入购物车?",{icon:3,title:'购物车提示'},function(index){
						$.getJSON("http://localhost:8080/addShoppingCar",{goodsId:obj.data.goodsId},function(d){
							if(d.code!=200){
								layer.msg(d.msg);
							}else{
								
								layer.close(index);
							}
						});
						
					});
										
				}
			})
			
			
			
			
			
		})
		
	</script>

</html>